﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Reflection;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Views;
using SharedGenomics.Workbench.Services;
using SharedGenomics.Domain;
using SharedGenomics.Annotations;
using SharedGenomics.Workbench.Messages;

namespace SharedGenomics.Workbench.Presenters
{
    public class WebBrowserPresenter : Presenter
    {
        Guid _viewedAnnotationId = Guid.Empty;

        public WebBrowserPresenter(IWebBrowserView view, IAnnotationNotifier annotationNotifier)
            : base(view)
        {
            //set private properties
            this.WebBrowserView = view;
            this.AnnotationNotifier = annotationNotifier;
            view.CommentButtonClicked += this.View_CommentButtonClicked;
            view.ImportanceChanged += View_ImportanceChanged;
            view.NavigationCompleted += new EventHandler<NavigationCompletedEventArgs>(this.View_NavigationCompleted);
        }

        private void View_NavigationCompleted(object sender, NavigationCompletedEventArgs e)
        {
            //displayed image has changed so create a new identifier for the current annotation
            this._viewedAnnotationId = Guid.NewGuid();

            //create annotation message and dispatch
            IAnnotation annotation = new UriAnnotation(e.Uri, e.DocumentTitle);
            this.AnnotationNotifier.CaptureAnnotation(this._viewedAnnotationId, annotation, e.PageImage);
        }

        private void View_CommentButtonClicked(object sender, EventArgs e)
        {
            // TODO: Write me
        }

        void View_ImportanceChanged(object sender, ImportanceChangedEventArgs e)
        {
            this.AnnotationNotifier.SetAnnotationImportance(this._viewedAnnotationId, e.Importance);
        }

        public void Navigate(Uri target)
        {
            WebBrowserView.Url = target.ToString();
        }

        public override bool SnapshotOnOpen
        {
            get { return false; }
        }

        private IWebBrowserView WebBrowserView { get; set; }
        private IAnnotationNotifier AnnotationNotifier { get; set; }
    }
}
